package com.fbinggun.fbinggunutil.rtp;


import com.fbinggun.fbinggunutil.API.BlockUtil;
import com.fbinggun.fbinggunutil.FBinggunUtil;
import com.fbinggun.fbinggunutil.API.RandomUtil;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;

/**
 * 随机坐标 随机传送方法
 */
public class Rtp {

    public static List<String> riskBlock;

    Location center;
    int radius;

    public Rtp(Location center, int radius) {
        this.center = center;
        this.radius = radius;
    }

    /**
     * 安全的随机传送 如果未找到安全位置 则不传送
     * @param biome 生物群筛选
     * @param entity 要传送的实体
     */
    public  void rtpPlayer(String biome, LivingEntity entity) {
        long time = new Date().getTime();
        new BukkitRunnable() {
            @Override
            public void run() {
                Location location = getLocation(biome);
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        if (location != null) {
                            entity.teleport(location);
                            entity.sendMessage("§a传送成功 耗时" + (new Date().getTime() - time) + "毫秒");
                        } else {
                            entity.sendMessage("§a传送取消 未找到安全传送点");
                        }
                    }
                }.runTask(FBinggunUtil.getPlugin(FBinggunUtil.class));
            }
        }.runTaskAsynchronously(FBinggunUtil.getPlugin(FBinggunUtil.class));
    }

    /**
     * 危险的随机传送 不考虑未找到位置的情况
     * @param entity 要传送的实体
     */
    public  void rtpRiskPlayer(LivingEntity entity) {
        long time = new Date().getTime();
        new BukkitRunnable() {
            @Override
            public void run() {
                Location location = getLocation();
                while (location==null){
                    location= getLocation();
                }
                Location finalLocation = location;
                new BukkitRunnable() {
                    @Override
                    public void run() {
                            entity.teleport(finalLocation);
                            entity.sendMessage("§a传送成功 耗时" + (new Date().getTime() - time) + "毫秒");

                    }
                }.runTask(FBinggunUtil.getPlugin(FBinggunUtil.class));
            }
        }.runTaskAsynchronously(FBinggunUtil.getPlugin(FBinggunUtil.class));
    }

    /**
     * 获得一个随机坐标 不做特殊计算 不计算高度
     * @return
     */
    private Location getRandomLocation() {
        Location location = center.clone();
        Double maxZ = location.getZ() + radius;
        Double maxX = location.getX() + radius;
        Double minZ = location.getZ() - radius;
        Double minX = location.getX() - radius;
        Double Z = RandomUtil.getDoubleRandom(maxZ, minZ);
        Double X = RandomUtil.getDoubleRandom(maxX, minX);
        location.setX(X);
        location.setZ(Z);
        return location;
    }

    /**
     * 获得一个随机坐标 取最高位置

     * @param biome 生物群筛选
     * @return
     */
    public Location getLocation(String biome) {
        return getLocation(biome,null);
    }
    /**
     * 获得一个随机坐标 取最高位置
     * @return
     */
    public Location getLocation() {
        return getLocation(null,null);
    }
    /**
     * 获得一个随机坐标 取最高位置
     * @param riskBlock 危险的方块
     * @return
     */
    public Location getLocation( List<String> riskBlock) {
        return getLocation(null,riskBlock);
    }
    /**
     * 获得一个随机坐标 取最高位置
     * @param riskBlock 危险的方块
     * @param biome 生物群筛选
     * @return
     */
    public Location getLocation( String biome, List<String> riskBlock) {
        int maxAttempts = 100;
        World world = center.getWorld();
        for (int i = 0; i < maxAttempts; i++) {
            Location randomLocation = getRandomLocation();
            int randomY = world.getHighestBlockYAt(randomLocation) + 1;
            randomLocation.setY(randomY);
            List<String> riskBlockList = new ArrayList<>();
            riskBlockList.addAll(getRiskBlock());
            if (riskBlock != null) {
                riskBlockList.addAll(riskBlock);
            }
            Block block = randomLocation.getBlock();
            if (riskBlockList.contains(BlockUtil.getType(block).toUpperCase())) {
                continue;
            }
            if (biome != null && !biome.equalsIgnoreCase(block.getBiome().toString())) {
                continue;
            }
            return randomLocation;
        }
        return null;
    }


    private List<String> getRiskBlock() {
        List<String> list = new ArrayList<>();
        list.add("WATER");
        list.add("LAVA");
        list.add("MAGMA_BLOCK");
        list.add("AZALEA_LEAVES");
        list.add("CHERRY_LEAVES");
        list.add("MANGROVE_LEAVES");
        list.add("DARK_OAK_LEAVES");
        list.add("JUNGLE_LEAVES");
        list.add("FLOWERING_AZALEA_LEAVES");
        list.add("BIRCH_LEAVES");
        list.add("SPRUCE_LEAVES");
        list.add("ACACIA_LEAVES");
        list.add("OAK_LEAVES");
        return list;
    }

}
